package www.shuang.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import www.shuang.common.util.PageInfoUtil;
import www.shuang.system.dao.IARegionDao;
import www.shuang.system.pojo.ARegion;
import www.shuang.system.service.IARegionService;


@Service("regionService")
public class RegionServiceImpl implements IARegionService
{	@Resource
	protected IARegionDao regionDao;
	@Override
	public ARegion findOneARegionService(Map<String, Object> condMap)
	{
		return this.regionDao.findOne(condMap);
	}

	@Override
	public List<ARegion> findCondListARegionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/*pageCount==true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		List<ARegion> regionList=new ArrayList<ARegion>();
		//处理关键字
		if(condMap.get("keyword") != null){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil != null){
			//分页查询
			condMap.put("pageCount", "true");
			regionList=this.regionDao.findCondList(condMap);
			//分页,总记录数放在了id属性上
			pageInfoUtil.setTotalRecord(regionList.get(0).getId());
			//拿分页数据
			condMap.put("pageCount","false");
			//每页有多少记录
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			regionList=this.regionDao.findCondList(condMap);
			
		}else{
			regionList=this.regionDao.findCondList(condMap);
		}
		
		
		return regionList;
	}
	

	@Override
	public JSONObject insertOneARegionService(ARegion region)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.regionDao.insert(region);
	if(res>0){
		resultJSON.put("code", "0");
		resultJSON.put("info", "添加成功");
		//添加二级json
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("affect",res);
		resultJSON.put("data", dataJSON);
	}else{
		resultJSON.put("code", "1");
		resultJSON.put("info", "添加失败");
	}
	
	return resultJSON;
	}

	@Override
	public JSONObject updateOneARegionService(ARegion region)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.regionDao.update(region);
	if(res>0){
		resultJSON.put("code", "0");
		resultJSON.put("info", "更新成功");
		//添加二级json
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("affect",res);
		resultJSON.put("data", dataJSON);
	}else{
		resultJSON.put("code", "1");
		resultJSON.put("info", "更新失败");
	}
	
	return resultJSON;
	}

	@Override
	public JSONObject deleteOneARegionService(ARegion region)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
